﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LibraryO2S;
using System.IO;

public partial class Administrator_popupadd : System.Web.UI.Page
{
    private PopupController objPopup = new PopupController();
    string m_sPath = AppDomain.CurrentDomain.BaseDirectory;
    string m_sPath2 = AppDomain.CurrentDomain.BaseDirectory;
    Random rd = new Random();
    string m_sFileAddRandom = "";
    List<string> m_lstValidFile = new List<string>();
    private BusinessController objBusinessController = new BusinessController();
    private ConnectSQL objConnect = new ConnectSQL();
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        string desc = txtContent.Text;
        string m_sFileName = "";
        string status = "";
        if (chkStatus.Checked == true)
        {
            status = "1";
        }
        else
        {
            status = "0";
        }
        m_sPath = m_sPath + "imgpopup";
        foreach (string m_sFileUpload in Request.Files)
        {
            m_lstValidFile.Clear();
            m_lstValidFile.Add(".jpg");
            m_lstValidFile.Add(".jpeg");
            m_lstValidFile.Add(".png");
            m_lstValidFile.Add(".gif");

            m_sFileName = Path.GetFileName(Request.Files[m_sFileUpload].FileName);

            m_sFileAddRandom = rd.Next(1, 9000).ToString().Replace(".", "") + m_sFileName;

            if (!String.IsNullOrEmpty(m_sFileName))
            {
                if (!objBusinessController.allowFile(m_lstValidFile, m_sFileName))
                {
                    divMessage.InnerHtml = O2S_Message.Warning("Cảnh Báo", "Chỉ được phép upload các file '.jpg, .gif, .png'");
                    return;
                }

                Request.Files[m_sFileUpload].SaveAs(Path.Combine(m_sPath, m_sFileAddRandom));
            }
        }
        if (status == "1")
        {
            objConnect.ExcutedCMD("update tblPopup set STATUS = 0 where STATUS = 1");
        }
        int a = objPopup.Insert(m_sFileAddRandom, desc, status);
        if (a > 0)
        {
            Response.Redirect("popup.aspx");
        }

    }
}